<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/15
 * Time: 17:17
 */

namespace app\admin\controller;


use app\admin\helper\SearchHelper;
use app\admin\model\novel\NovelChapterM;
use app\admin\model\novel\NovelM;
use app\admin\model\novel\NovelVolumeM;
use app\admin\model\TagM;
use think\Config;
use think\File;

class Novel extends Base
{

    protected $novelM;
    protected $tagM;
    protected $novelVolumeM;
    protected $novelChapterM;

    protected $novelTag;
    /*
     * 依赖注入Model
     * */
    public function __construct(NovelM $novelM,TagM $tagM,NovelVolumeM $novelVolumeM,NovelChapterM $novelChapterM){
        parent::__construct();
        $this->novelM = $novelM;
        $this->tagM = $tagM;
        $this->novelVolumeM = $novelVolumeM;
        $this->novelChapterM = $novelChapterM;
        $this->novelTag = array_keys(Config::get('app.tag.category'))[0];
    }

    /**
     * 列表
     */
    public function novel_chapter_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->novelChapterM->listData();
        }
        $this->assign("__search",$this->novelChapterM->__search());
        $this->assign("__header",$this->novelChapterM->__header());
        return $this->fetch('novel/chapter_list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function novel_chapter_form(){
        if ($this->request->isPost()) {
            $change = input('post.change/b');
            if ($change) {
                return $result=[
                    'code'=>1,
                    'data'=> $this->novelVolumeM->listAll(input('post.novel_id/d'))
                ];
            }else {
                $method = input('post.method/s');
                $validate = $this->novelChapterM->validate_model();
                if ($validate['code']){
                    if ($method == 'edit') {
                        return $this->novelChapterM->edit();
                    }else if ($method == 'add') {
                        return $this->novelChapterM->add();
                    }
                }else{
                    return $validate;
                }
            }
            return op_fail();
        }
        $novel_list = $this->novelM->listAll();
        $data = $this->novelChapterM->getById(input('get.id/d'));
        $method = input('get.method/s');
        if ($method == 'edit') {
            $novel_id = $data['novel_id'];
        }else {
            $novel_id = $novel_list->toArray()[0]['id'];
        }
        $this->assign("novel_list",$this->novelM->listAll());
        $this->assign("volume_list",!empty($novel_list)?$this->novelVolumeM->listAll($novel_id):[]);
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",$method);
        $this->assign("data",$data);
        return $this->fetch('novel/chapter_form');
    }

    /**
     * 删除
     */
    public function novel_chapter_del(){
        return $this->novelChapterM->del();
    }

    /**
     * 列表
     */
    public function novel_volume_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->novelVolumeM->listData();
        }
        $this->assign("__search",$this->novelVolumeM->__search());
        $this->assign("__header",$this->novelVolumeM->__header());
        return $this->fetch('novel/volume_list');
    }


    /**
     * @return array|mixed
     */
    public function novel_volume_relevance_list(){
        $novel_id = input('post.value/d');
        return [
            'code'=>1,
            'data'=> $this->novelVolumeM->listAll($novel_id),
        ];
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function novel_volume_form(){
        if ($this->request->isPost()) {
            $change = input('post.change/b');
            if ($change) {
                return $result=[
                    'code'=>1,
                    'data'=> $this->novelVolumeM->getLastSort(input('post.novel_id/d'))
                ];
            }else {
                $method = input('post.method/s');
                $validate = $this->novelVolumeM->validate_model();
                if ($validate['code']){
                    if ($method == 'edit') {
                        return $this->novelVolumeM->edit();
                    }else if ($method == 'add') {
                        return $this->novelVolumeM->add();
                    }
                }else{
                    return $validate;
                }
            }
            return op_fail();
        }
        $novel_list = $this->novelM->listAll();
        $this->assign("novel_list",$this->novelM->listAll());
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->novelVolumeM->getById(input('get.id/d')));
        $this->assign("lastSort",$this->novelVolumeM->getLastSort(array_column($novel_list->toArray(),'id')[0]));
        return $this->fetch('novel/volume_form');
    }

    /**
     * 删除
     */
    public function novel_volume_del(){
        return $this->novelVolumeM->del();
    }

    /**
     * 列表
     */
    public function novel_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            $tags = $this->tagM->listAll($this->novelTag)->toArray();
            $tags = array_column($tags,'name','code');
            return $this->novelM->listData($tags);
        }
        $this->assign("__search",$this->novelM->__search());
        $this->assign("__header",$this->novelM->__header());
        return $this->fetch('novel/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function novel_form(){
        if ($this->request->isPost()) {
            $validate = $this->novelM->validate_model();
            if ($validate['code'] ){
                $files = $this->request->file('image');
                if (is_array($files)) {
                    /**
                     * @var File $file
                     */
                    foreach ($files as $file){
                        $path = ROOT_PATH . 'public' . DS . 'uploads'.DS.'novel_thumbnail'.DS;
                        file_exists($path)?:mkdir($path, 0755, true);
                        $url='/uploads/novel_thumbnail/';
                        $info = $file->validate(['ext'=>'gif,jpg,jpeg,bmp,png'])->rule('uniqid')->move($path);
                        if($info){
                            $name = $info->getFilename();
                            $data['ab_icon']=$path.$info->getSaveName();
                            $data['icon'] = $url.$name;
                            unset($data['image']);
                            $this->request->post($data);
                        }
                    }
                }
                $method = input('post.method/s');
                if ($method == 'edit') {
                    return $this->novelM->edit();
                }else if ($method == 'add') {
                    return $this->novelM->add();
                }
            }
            return $validate;
        }
        $this->assign("lang_field",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $tags = $this->tagM->listAll($this->novelTag)->toArray();
        $tags = array_column($tags,'name','code');
        $this->assign("tags_list",$tags);
        $novel_status = SearchHelper::getNovelStatusList();
        unset($novel_status[0]);
        $this->assign("novel_status",$novel_status);
        $this->assign("data",$this->novelM->getById(input('get.id/d')));
        return $this->fetch('novel/form');
    }

    /**
     * 删除
     */
    public function novel_del(){
        return $this->novelM->del();
    }

}